package tests;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;

public class ByteBufferTest {

        public static void main(String[] args) {

            // Allocate a new non-direct byte buffer with a 20 byte capacity
            // The underlying storage is a byte array.
            ByteBuffer buf = ByteBuffer.allocate(20);

            
            // Get the buffer's capacity
            int capacity = buf.capacity();

            // Get the buffer's limit
            int limit = buf.limit();

            // Get the buffer's position
            int position = buf.position();

            System.out.println("Buffer capacity: " + capacity);
            System.out.println("Buffer limit: " + limit);
            System.out.println("Buffer position: " + position);

            buf.put((byte)0x01); // at position 0

            position = buf.position();
            System.out.println("Buffer position: " + position);

            // Set the position
            buf.position(3);

            position = buf.position();
            System.out.println("Buffer position: " + position);

            // Use the relative put()
            buf.put((byte)0x02);

            position = buf.position();
            System.out.println("Buffer position: " + position);
            
            String hello = "hell, shane";
            buf.put(hello.getBytes());
            // Get remaining byte count
            int remainingBytes = buf.remaining();
            System.out.println("Buffer remaining bytes: " + remainingBytes);

            buf.position(6);
            
            ByteBuffer newbuf = ByteBuffer.allocate(20);
            ByteBuffer buf1 = buf.duplicate();
            ByteBuffer buf2 = buf.slice();
            System.out.println("buf1 capacity: " + buf1.capacity());
            System.out.println("buf2 capacity: " + buf2.capacity());
            System.out.println("buf2 position : " + buf2.position());
            newbuf.put(buf2);
            System.out.println("buf2 length : " + buf2.array().length);
            System.out.println("newbuf position : " + newbuf.position());
            System.out.println("newbuf limit : " + newbuf.limit());
            newbuf.flip();
            System.out.println("newbuf limit2 : " + newbuf.limit());
            System.out.println("newbuf : " + bytebuffer2String(newbuf));
            
            System.out.println("buf2 : " + bytebuffer2String(buf2));
            
            // Rewinds this buffer. The position is set to zero and the mark is discarded
            buf.rewind();

            ByteBuffer buf3 = buf.duplicate();
            System.out.println("buf3 capacity: " + buf3.capacity());
            ByteBuffer buf4 = buf.slice();
            System.out.println("buf4 capacity: " + buf4.capacity());
            
            remainingBytes = buf.remaining();
            System.out.println("Buffer remaining bytes: " + remainingBytes);
 
            

        }
        
        public static String bytebuffer2String(ByteBuffer buf) {
            byte[] bytes = buf.array();
            String str = new String(bytes, Charset.forName("UTF-8") );
            return str;
        }
}
